package com.punai.monthanalysis.bo;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.multi.ListValueMap;
import cn.hutool.core.util.StrUtil;
import com.punai.common.utils.bean.BeanUtils;
import com.punai.monthanalysis.domain.ProYdcbfxGcjdkhrgfdbfxb;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class ProYdcbfxGcjdkhrgfdbfxbBo extends ProYdcbfxGcjdkhrgfdbfxb{

    private Integer rowspan = 1;
    private Integer colspan = 1;

    public Integer getRowspan() {
        return rowspan;
    }

    public void setRowspan(Integer rowspan) {
        this.rowspan = rowspan;
    }

    public Integer getColspan() {
        return colspan;
    }

    public void setColspan(Integer colspan) {
        this.colspan = colspan;
    }

    public void clearColRow() {
        this.setRowspan(0);
        this.setColspan(0);
    }

    public static List<ProYdcbfxGcjdkhrgfdbfxbBo> convert(List<ProYdcbfxGcjdkhrgfdbfxb> inList) {
        List<ProYdcbfxGcjdkhrgfdbfxbBo> result = new ArrayList<>();
        if (CollUtil.isEmpty(inList)) {
            return result;
        }

        ListValueMap<String,ProYdcbfxGcjdkhrgfdbfxb> levelMap = new ListValueMap<>();
        for (ProYdcbfxGcjdkhrgfdbfxb gcjdkhrgfdbfxb : inList) {
            levelMap.putValue(StrUtil.blankToDefault(gcjdkhrgfdbfxb.getPid(),"0"),gcjdkhrgfdbfxb);
        }
        List<ProYdcbfxGcjdkhrgfdbfxb> roots = levelMap.get("0");
        roots.sort(Comparator.comparing(ProYdcbfxGcjdkhrgfdbfxb::getXh));
        // 只拆解两层
        for (ProYdcbfxGcjdkhrgfdbfxb root : roots) {
            String id = root.getId();
            ProYdcbfxGcjdkhrgfdbfxbBo convert = convert(root);
            List<ProYdcbfxGcjdkhrgfdbfxb> children = levelMap.get(id);
            if (CollUtil.isNotEmpty(children)) {
                convert.setRowspan(children.size() + 1);

                result.add(convert);
                children.sort(Comparator.comparing(ProYdcbfxGcjdkhrgfdbfxb::getXh));

                for (ProYdcbfxGcjdkhrgfdbfxb child : children) {
                    ProYdcbfxGcjdkhrgfdbfxbBo childrenBo = convert(child);
                    childrenBo.clearColRow();
                    result.add(childrenBo);
                }
            } else {
                result.add(convert);
            }
        }
        return result;
    }

    public static ProYdcbfxGcjdkhrgfdbfxbBo convert(ProYdcbfxGcjdkhrgfdbfxb in) {
        ProYdcbfxGcjdkhrgfdbfxbBo out = new ProYdcbfxGcjdkhrgfdbfxbBo();
        BeanUtils.copyBeanProp(out,in);
        return out;
    }

}
